﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace wspol_zad3
{
    public partial class Form1 : Form
    {
        Buffors b;
        ThreadMenager tm;
        string text;

        public Form1()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            if (tm != null)
            {
                tm.ShutDown();
            }

            Application.Exit();
        }

        private void RefrehTextBox()
        {
            while (true)
            {
                if (b != null)
                {
                    text = b.PrepareStringData();
                }
                Thread.Sleep(25);
            }
        }

        private void runButton_Click(object sender, EventArgs e)
        {
            int b1Cnt = (int)b1Num.Value;
            int b2Cnt = (int)b2Num.Value;

            b = new Buffors(b1Cnt, b2Cnt);
            tm = new ThreadMenager(b);

            Thread p1 = new Thread(new ParameterizedThreadStart(tm.Producer));
            Thread p2 = new Thread(new ParameterizedThreadStart(tm.Producer));

            Thread c = new Thread(new ParameterizedThreadStart(tm.FromB1ToB2));

            Thread s = new Thread(new ThreadStart(tm.ClearB2));

            Thread refresh = new Thread(new ThreadStart(RefrehTextBox));

            p1.Start(1000);
            p2.Start(2000);
            c.Start(1500);
            s.Start();
            //refresh.Start();

            while (true)
            {
                if (b != null)
                {
                    text = b.PrepareStringData();
                    textBox1.Clear();
                    textBox1.Text = text;
                }

                this.Refresh();
                Thread.Sleep(25);
            }

        }
    }
}
